/*
 * buffer.cxx
 *
 *  Created on: Apr 4, 2011
 *      Author: whisper
 */

#include "crypto/buffer.h"

/**
 * @brief Dynamic push and pop Buffer for audio samples
 * @author Bernhard Schildendorfer
 *
 * CBuffer provides a fixed length byte-buffer which indicates if it's full.
 *
 * CBuffer contains two FIFO Queues, which are called input-buffer and output-buffer.
 * Data which is written to CBuffer is stored to the input-buffer, data which is
 * read from CBuffer is read from the output-buffer. To transfer data between them,
 * the method processBuffer() needs to be called. Manipulation to the buffer is
 * possible via the byte* of isBufferReady();
 *
 */
//fixed size of the buffer
CBuffer::CBuffer() {
	//mInputBuffer = new queue<double>();
	//mOutputBuffer = new queue<double>();
}




CBuffer::~CBuffer() {
	// TODO Auto-generated destructor stub
	// nothing to delete yet
}






//store pCount bytes in the Buffer.
//return: - if the buffer is full, a  byte pointer is returned to the beginning of the full buffer
//        - null if buffer is not full
void CBuffer::write(const double* pData , unsigned int pCount){
	if(pCount==0 || pData==NULL)
		return;

	for(unsigned int i=0;i<pCount;i++){
		//if(	i==150)
		//	i=150;
		mInputBuffer.push(pData[i]);
	}
	return;
}



//receive and remove pCount elements from the output buffer.
//If there is less data in the buffer than requested by pCount, additional zeros will be written
void CBuffer::read(double* pData, unsigned int pCount){
	if(pCount==0 || pData==NULL)
		return;

	for(unsigned int i=0;i<pCount;i++){
		if(mOutputBuffer.size()==0)
			pData[i]=0;
		else
		{
			pData[i]=mOutputBuffer.front();
			mOutputBuffer.pop();
		}
	}
}




//Get pCount of the unprocessed Buffer. Elements of the InputBuffer
//are deleted.
double* CBuffer::getUnprocessedBuffer(unsigned int pCount){
	if(pCount==0)
		return NULL;
	if(pCount>mInputBuffer.size())
		return NULL;

	double* data = new double[pCount];
	for(unsigned int i=0;i<pCount;i++){
		data[i]=mInputBuffer.front();
		mInputBuffer.pop();
	}
	return data;
}


//Writes pCount data to the output-buffer
void CBuffer::putProcessedBuffer(double* pData, unsigned int pCount){
	if(pCount==0 || pData==NULL)
		return;

	for(unsigned int i=0;i<pCount;i++){
		mOutputBuffer.push(pData[i]);
	}
	return;
}


//Returns the number of Elements in the input-buffer
unsigned int CBuffer::getInputBufferSize(){
	return mInputBuffer.size();
}

//Returns the number of Elements in the input-buffer
unsigned int CBuffer::getOuputBufferSize(){
	return mOutputBuffer.size();
}




